package com.li.test.base;

public class VoliatileDemo {
    public volatile Integer num = 0;

    public  void numPlusPlus() {
        num++;
    }

    public static void main(String[] args) {
        VoliatileDemo vd = new VoliatileDemo();
        for (int i = 1; i <= 20; i++
        ) {
            new Thread(()->{
                for (int j = 1; j <= 1000; j++){
                    vd.numPlusPlus();
                }
            }).start();
            System.out.println(Thread.activeCount());
        }
       if(Thread.activeCount()>1 ){
            Thread.yield();
        }

       /*try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        System.out.println("输出前线程数："+Thread.activeCount());
        System.out.println("输出num："+vd.num);
    }
}
